package com.xzx.androidutil;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import com.xzx.androidutil.entity.AppInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2017/3/2 22:48
 */
public class AppUtil {
    private AppUtil(){}
    private static final String TAG = AppUtil.class.getName();

    /**
     * 获取所有应用列表
     * @param context .
     * @return .
     */
    public static List<AppInfo> getAppList(Context context) {
        List<AppInfo> appInfos = new ArrayList<>();
        //得到应用的packageManager
        PackageManager packageManager = context.getPackageManager();
        //创建一个住建面的Intent
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        //得到包含应用信息的列表
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
        //遍历
        for (ResolveInfo resolveInfo : resolveInfos) {
            //包名
            String packageName = resolveInfo.activityInfo.packageName;
            //得到图标
            Drawable icon = resolveInfo.loadIcon(packageManager);
            //应用名称
            String appName = resolveInfo.loadLabel(packageManager).toString();

            AppInfo appInfo = new AppInfo(icon, appName, packageName);
            Log.i(TAG, "应用：" + appInfo);
            appInfos.add(appInfo);
        }
        return appInfos;
    }

    /**
     * 安装apk文件：到安装界面
     * @param apkFile
     */
    public static void installApk(Context context, File apkFile) {
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 将隐式Intent转为显式Intent
     * @param context
     * @param implicitIntent
     * @return
     */
    public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }

        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);

        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);

        // Set the component to be explicit
        explicitIntent.setComponent(component);

        return explicitIntent;
    }
}
